package com.xn.app.muying.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.xn.app.muying.R;
import com.xn.app.muying.util.AndroidBug54971Workaround;
import com.xn.app.muying.util.BaseActivity;
import com.xn.app.muying.util.Constants;

import java.util.Timer;
import java.util.TimerTask;

public class HuanYingActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidBug54971Workaround.assistActivity(findViewById(android.R.id.content));
        //查看是否登录过
        boolean lean = Constants.getBooleanByName(this, "firstLogin");
        //如果手机系统大于等于5.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
//            window.setNavigationBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //如果手机系统大于等于4.4并且小于等于5.0
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        if (lean) {
            //正常登录
            setContentView(R.layout.activity_huan_ying);
            final Intent it = new Intent(this, MainActivity.class); //你要转向的Activity
            Timer timer = new Timer();
            TimerTask task = new TimerTask() {
                @Override
                public void run() {
                    startActivity(it); //执行
                    try {
                        Thread.sleep(1000);
                        finish();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            timer.schedule(task, 1000 * 3); //2秒后
        } else {
            //首次登录
            setContentView(R.layout.activity_first_ligin);
            // 加载图片
            int[] list = {R.mipmap.first1, R.mipmap.first2, R.mipmap.first3, R.mipmap.first4};
            // 初始化组件
            ViewPager view_pager = (ViewPager) findViewById(R.id.view_pager);
            // 加载图片
            view_pager.setAdapter(new ImageAdapter(list, this));
        }
    }

    /**
     * view_pager的适配器
     *
     * @author Android
     */
    private class ImageAdapter extends PagerAdapter {

        private LayoutInflater inflater;
        private int[] list;

        ImageAdapter(int[] list, Activity activity) {
            inflater = LayoutInflater.from(activity);
            this.list = list;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
        }

        @Override
        public int getCount() {
            return list.length;
        }

        @Override
        public Object instantiateItem(ViewGroup view, int position) {
            View imageLayout = inflater.inflate(R.layout.pager_item_image,
                    view, false);
            ImageView imageView = (ImageView) imageLayout
                    .findViewById(R.id.image);
            imageView.setImageResource(list[position]);
            if (position == list.length - 1) {
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Constants.setBooleanByName(HuanYingActivity.this, "firstLogin", true);
                        startActivity(new Intent(HuanYingActivity.this, MainActivity.class));
                        finish();
                    }
                });
            }
            view.addView(imageLayout, 0);
            return imageLayout;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view.equals(object);
        }

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
